package com.xingyu.springbootlearn.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @Author 刘星宇
 * @Date 2020/10/5 14:54
 */
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {

        String language = httpServletRequest.getParameter("l"); //获取前端传入参数 l（语言地区类型）
        Locale locale = Locale.getDefault(); //如果没有指定，则使用默认
        if(!StringUtils.isEmpty(language)){
            String[] split = language.split("_");//zh_CN
            new Locale(split[0], split[1]); //国家_地区
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
